<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="/easyui.css">
    <link rel="stylesheet" type="text/css" href="/icon.css">
    <link rel="stylesheet" type="text/css" href="/demo.css">
    <script src="/jquery.min.js"></script>
    <script src="/jquery.easyui.min.js"></script>
    <meta charset="UTF-8">
    <title>班级学生表</title>
</head>
<body>
<div>
    <h2>班级列表：</h2>
    <table id="banji"></table>
    <h2>学生列表：</h2>
    <button id="button" value="分配">给班级分配学生</button>
    <table id="student"></table>
</div>
<script type="text/javascript">
    $("#banji").datagrid({
        url:"/BanjiStudentController/banjiDate",
        singleSelect: true,
        rownumbers: "true",
        fitColumns: "true",
        pagination: "true",
        pageSize: 10,
        columns: [[
            {field: 'bid', title: '编号', width: 100},
            {field: 'bname', title: '班级', width: 100}
        ]]
    });
    var banjistudent = [];
    $("#student").datagrid({
        url:'/BanjiStudentController/studentDate',
        rownumber:'true',
        fitColumns:'true',
        pagination:"true",
        pageSize:10,
        columns:[[
            {field:'sid',title:'学号',with:100},
            {field:'sname',title:'名字',width:100},
            {field:'ssex',title:'性别',width:50},
            {field:'bid',title:'班级编号',width:50}
        ]],
        onLoadSuccess:function(data){
            $.each(data.rows,function(index,value){
                $.each(banjistudent,function(j,v){
                    if(value.sid == v.sid){
                        $("#student").datagrid('selectRow',index);
                    }
                });
            });
        }
    });
    var nid = "";
    $("#banji").datagrid({
        onClickRow:function(rowIndex, rowData){
            bid = rowData.bid;
            $.post('/BanjiStudentController/banjiShowStudent',{bid:rowData.bid},function (data) {
                console.log(data);
                banjistudent = data;
                $("#student").datagrid('reload');
            });
        }
    });
    var str = [];
    $("#button").click(function () {
//        获取表格中被选中的多行数据
        rows = $("#student").datagrid('getSelections');
        $.each(rows,function (i,v) {
            str[i] = rows[i].sid;
        });
        console.log(str);
        $.post("/BanjiStudentController/allSelectedStudent",{bid:bid,str:str});
    });
//分页
    function pagerFilter(data){
        if (typeof data.length == 'number' && typeof data.splice == 'function'){	// is array
            data = {
                total: data.length,
                rows: data
            }
        }
        var dg = $(this);
        var opts = dg.datagrid('options');
        var pager = dg.datagrid('getPager');
        pager.pagination({
            onSelectPage:function(pageNum, pageSize){
                opts.pageNumber = pageNum;
                opts.pageSize = pageSize;
                pager.pagination('refresh',{
                    pageNumber:pageNum,
                    pageSize:pageSize
                });
                dg.datagrid('loadData',data);
            }
        });
        if (!data.originalRows){
            data.originalRows = (data.rows);
        }
        var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
        var end = start + parseInt(opts.pageSize);
        data.rows = (data.originalRows.slice(start, end));
        return data;
    }
    $(function(){
        $('#student').datagrid({loadFilter:pagerFilter});
    });
    $(function () {
        $("#banji").datagrid({loadFilter:pagerFilter})
    })

</script>

</body>
</html>